home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE09 / FILES / STRM1U.PAS < prev   
Pascal/Delphi Source File  |  1996-02-27  |  3KB  |  169 lines

  1. unit Strm1u;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls, Buttons;
  8.  
  9. type
  10.   TPointData = class
  11.   public
  12.     X, Y: Word;
  13.     constructor CreateXY(AX, AY: Word);
  14.     procedure SwapXY;
  15.     procedure LoadFromStream(Stream: TStream); virtual;
  16.     procedure SaveToStream(Stream: TStream); virtual;
  17.   end;
  18.  
  19.   TForm1 = class(TForm)
  20.     PaintBox1: TPaintBox;
  21.     Bevel1: TBevel;
  22.     MakeBtn: TButton;
  23.     SaveBtn: TButton;
  24.     LoadBtn: TButton;
  25.     SwapBtn: TButton;
  26.     procedure FormCreate(Sender: TObject);
  27.     procedure FormDestroy(Sender: TObject);
  28.     procedure PaintBox1Paint(Sender: TObject);
  29.     procedure MakeBtnClick(Sender: TObject);
  30.     procedure SaveBtnClick(Sender: TObject);
  31.     procedure LoadBtnClick(Sender: TObject);
  32.     procedure SwapBtnClick(Sender: TObject);
  33.   private
  34.     PointList: TList;
  35.     procedure ClearPoints;
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.   Pt: TPointData;
  41.   Loop: Integer;
  42.  
  43. const
  44.   DataFile = 'POINTS1.DAT';
  45.  
  46. implementation
  47.  
  48. {$R *.DFM}
  49.  
  50. constructor TPointData.CreateXY(AX, AY: Word);
  51. begin
  52.   inherited Create;
  53.   X := AX;
  54.   Y := AY;
  55. end;
  56.  
  57. procedure TPointData.SwapXY;
  58. var
  59.   Tmp: Word;
  60. begin
  61.   Tmp := X;
  62.   X := Y;
  63.   Y := Tmp;
  64. end;
  65.  
  66. procedure TPointData.LoadFromStream(Stream: TStream);
  67. begin
  68.   Stream.Read(X, SizeOf(X));
  69.   Stream.Read(Y, SizeOf(Y));
  70. end;
  71.  
  72. procedure TPointData.SaveToStream(Stream: TStream);
  73. begin
  74.   Stream.Write(X, SizeOf(X));
  75.   Stream.Write(Y, SizeOf(Y));
  76. end;
  77.  
  78. procedure TForm1.ClearPoints;
  79. begin
  80.   while PointList.Count > 0 do
  81.   begin
  82.     TPointData(PointList[0]).Free;
  83.     PointList.Delete(0);
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.FormCreate(Sender: TObject);
  88. begin
  89.   PointList := TList.Create;
  90. end;
  91.  
  92. procedure TForm1.FormDestroy(Sender: TObject);
  93. begin
  94.   ClearPoints;
  95.   PointList.Free;
  96. end;
  97.  
  98. procedure TForm1.PaintBox1Paint(Sender: TObject);
  99. begin
  100.   for Loop := 0 to PointList.Count - 1 do
  101.   begin
  102.     Pt := TPointData(PointList.Items[Loop]);
  103.     if Loop = 0 then
  104.       PaintBox1.Canvas.MoveTo(Pt.X, Pt.Y)
  105.     else
  106.       PaintBox1.Canvas.LineTo(Pt.X, Pt.Y)
  107.   end;
  108. end;
  109.  
  110. procedure TForm1.MakeBtnClick(Sender: TObject);
  111. begin
  112.   ClearPoints;
  113.   for Loop := 1 to Random(40) + 1 do
  114.   begin
  115.     Pt := TPointData.CreateXY(Random(PaintBox1.Width),
  116.                             Random(PaintBox1.Height));
  117.     PointList.Add(Pt);
  118.     PaintBox1.Invalidate;
  119.   end;
  120. end;
  121.  
  122. procedure TForm1.SaveBtnClick(Sender: TObject);
  123. var
  124.   Stream: TFileStream;
  125. begin
  126.   Stream := TFileStream.Create(DataFile, fmCreate);
  127.   try
  128.     for Loop := 0 to PointList.Count - 1 do
  129.     begin
  130.       Pt := TPointData(PointList.Items[Loop]);
  131.       Pt.SaveToStream(Stream);
  132.     end;
  133.   finally
  134.     Stream.Free;
  135.   end;
  136.   ClearPoints;
  137.   PaintBox1.Invalidate;
  138. end;
  139.  
  140. procedure TForm1.LoadBtnClick(Sender: TObject);
  141. var
  142.   Stream: TFileStream;
  143. begin
  144.   ClearPoints;
  145.   Stream := TFileStream.Create(DataFile, fmOpenRead or fmShareDenyWrite);
  146.   try
  147.     while Stream.Position <> Stream.Size do
  148.     begin
  149.       Pt := TPointData.Create;
  150.       Pt.LoadFromStream(Stream);
  151.       PointList.Add(Pt);
  152.     end;
  153.   finally
  154.     Stream.Free;
  155.   end;
  156.   PaintBox1.Invalidate;
  157. end;
  158.  
  159. procedure TForm1.SwapBtnClick(Sender: TObject);
  160. begin
  161.   for Loop := 0 to PointList.Count - 1 do
  162.     TPointData(PointList.Items[Loop]).SwapXY;
  163.   PaintBox1.Invalidate;
  164. end;
  165.  
  166. initialization
  167.   Randomize;
  168. end.
  169.